﻿using BCCommon;
using BCCommon.Enums;
using BCDto.Common.ErrorMessage;
using BCDto.TMS.HealthReport;
using BCService.TMS.HealthReport;
using BCTMSRESTful.Controllers.Management;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;


namespace BlackCoffeeRESTful.Controllers.Management.TMS.HealthReport
{
    /// <summary>
    /// 健康上报控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class HealthReportController : ManagementBaseController
    {
        private readonly IHealthReportService healthReportService;

        public HealthReportController(IHealthReportService healthReportService)
        {
            this.healthReportService = healthReportService;
        }

        /// <summary>
        /// 获取健康上报列表
        /// </summary>
        /// <param name="start">上报时间开始时间</param>
        /// <param name="end">上报时结束时间</param>
        /// <param name="role">角色</param>
        /// <param name="isFever">是否发烧</param>
        /// <param name="isCoughs">是否咳嗽</param>
        /// <param name="userId">人员ID</param>
        /// <param name="ascending">是否正序</param>
        /// <param name="index">页号</param>
        /// <param name="size">每页数据条数</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<HealthReportDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult List([FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            [FromQuery(Name = "role")] RoleType? role,
            [FromQuery(Name = "is_fever")] bool? isFever,
            [FromQuery(Name = "is_coughs")] bool? isCoughs,
            [FromQuery(Name = "user_id")] long? userId,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "index")] int? index = 1,
            [FromQuery(Name = "size")] int? size = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                return this.Ok(this.healthReportService.GetList((long?)null, start, end, ascending, index, size, userId, null, isFever, isCoughs));
            }
            return this.Ok(this.healthReportService.GetList(this.Admin.AgentRecord == null ? this.Admin.CompanyId : this.Admin.AgentRecord.CompanyId, start, end, ascending, index, size, this.Admin.AdminId, this.Admin.AgentRecord == null ? RoleType.Admin : RoleType.Driver, isFever, isCoughs));
        }

        /// <summary>
        /// 获取健康上报统计列表
        /// </summary>
        /// <param name="timeSpan">时间间隔（天）</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(HealthReportAnalysisViewDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult AnalysisList([FromQuery(Name = "time_span")] int? timeSpan = 30)
        {
            return this.Ok(this.healthReportService.GetAnalysisList(timeSpan));
        }

        /// <summary>
        /// 获取个人健康上报阈值列表
        /// </summary>
        /// <param name="timeSpan">时间间隔（天）</param>
        /// <param name="role">角色</param>
        /// <param name="userId">人员ID</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<HealthReportUserDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult AnalysisUserList(
            [FromQuery(Name = "role")] RoleType role,
            [FromQuery(Name = "user_id")] long userId,
            [FromQuery(Name = "time_span")] int? timeSpan = 30)
        {
            return this.Ok(this.healthReportService.GetAnalysisUserList(timeSpan, role, userId));
        }
    }
}
